import { fireEvent, render, screen } from '@app-nextmesh/utils';
import '@testing-library/jest-dom';

import ValueInput from './ValueInput';

describe('ValueInput', () => {
  let newValue: number | boolean = 123;
  const onInputChange = (value: number | boolean) => {
    console.log('value', value);
    newValue = value;
  };
  test('error type', async () => {
    newValue = 1357;
    render(
      <ValueInput
        disabled={false}
        type='in'
        value={newValue}
        onChange={onInputChange}
      />,
    );
    expect(screen.getByRole('alert')).toHaveTextContent('未知类型');
  });
  test('error double', async () => {
    render(
      <ValueInput
        disabled={false}
        type='double'
        value={true}
        onChange={onInputChange}
      />,
    );
    expect(screen.getByRole('alert')).toHaveTextContent(
      '格式错误，必须是一个数字',
    );
  });
  test('error int', async () => {
    render(
      <ValueInput
        disabled={false}
        type='int'
        value={true}
        onChange={onInputChange}
      />,
    );
    expect(screen.getByRole('alert')).toHaveTextContent(
      '格式错误，必须是一个整数',
    );
  });
  test('error boolean', async () => {
    render(
      <ValueInput
        disabled={false}
        type='boolean'
        value={123}
        onChange={onInputChange}
      />,
    );
    expect(screen.getByRole('alert')).toHaveTextContent(
      '格式错误，必须是一个布尔值',
    );
  });
  test('fire event', async () => {
    newValue = false;
    render(
      <ValueInput
        disabled={false}
        type='boolean'
        value={newValue}
        onChange={onInputChange}
      />,
    );
    const input: HTMLInputElement = await screen.findByDisplayValue('false');
    fireEvent.change(input, { target: { value: 'true' } });
    expect(newValue).toBe(true);
  });
  test('rerender', async () => {
    newValue = false;
    const { rerender } = render(
      <ValueInput
        disabled={false}
        type='boolean'
        value={newValue}
        onChange={onInputChange}
      />,
    );
    newValue = 123.1;
    rerender(
      <ValueInput
        disabled={false}
        type='double'
        value={newValue}
        onChange={onInputChange}
      />,
    );
    expect(screen.getByDisplayValue(newValue.toString())).toBeInTheDocument();
  });
});
